package com.js.sys.fragment.view.third;

import android.app.Service;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.js.lib_common.base.MvpFragment;
import com.js.lib_common.event.StartBrotherEvent;
import com.js.lib_common.mvp.model.response.BindBean;
import com.js.lib_common.mvp.presenter.ProductPresenter;
import com.js.lib_common.mvp.view.ProductView;
import com.js.sys.BaseApplication;
import com.js.sys.R;
import com.js.sys.adapter.BindingAdapter;
import com.js.sys.fragment.view.first.BindHistoryFragment;
import com.js.sys.greendao.db.BindingHistory;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 入库
 * Created by wz on 2021/5/19.
 */
public class CheckInFragment extends MvpFragment<ProductPresenter> implements ProductView {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.rcy_list)
    RecyclerView rcyList;
    private BindingAdapter mAdapter;
    private static final int DEFAULT_VIEW = 0x22;
    private BindBean mBindBean;
    public static CheckInFragment newInstance() {
        Bundle args = new Bundle();
        CheckInFragment fragment = new CheckInFragment();
        fragment.setArguments(args);
        return fragment;

    }

    @Override
    protected ProductPresenter createPresenter() {
        return new ProductPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tab_third_checkin;
    }


    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        toolbarTitle.setText(R.string.tab_third);
        mAdapter = new BindingAdapter(_mActivity,null);
        initRecyclerView(rcyList, mAdapter, 1);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.im_del) {
                    mAdapter.getData().remove(position);
                    mAdapter.notifyDataSetChanged();
                }
            }
        });

        toolbar.inflateMenu(R.menu.menu_history);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                EventBus.getDefault().post(new StartBrotherEvent(BindHistoryFragment.newInstance(BindBean.REQUEST_CODE_CHECK_IN)));
                return false;
            }
        });
    }

    /**
     * 接收扫码后的信息
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiveQrcode(BindBean bean) {
        if (bean.getType()!= BindBean.REQUEST_CODE_CHECK_IN){
            return;
        }

        Vibrator vibrator = (Vibrator) _mActivity.getSystemService(Service.VIBRATOR_SERVICE);
        if (vibrator!=null)
            vibrator.vibrate(300);
        mAdapter.getData().clear();
        if (bean.getQrcode().length()==18) {
            mAdapter.addData(bean);
        }else {
            showTips("格式有误", "非整机ID");
        }


    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }


    @OnClick({R.id.rl_scan, R.id.rl_clear, R.id.rl_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_scan:
                scan(DEFAULT_VIEW, BindBean.REQUEST_CODE_CHECK_IN);
                break;

            case R.id.rl_clear:
                new XPopup.Builder(_mActivity)
                        .asConfirm("清空列表", "确认清空?",
                                "取消", "确定",
                                new OnConfirmListener() {
                                    @Override
                                    public void onConfirm() {
                                        mAdapter.getData().clear();
                                        mAdapter.notifyDataSetChanged();
                                    }
                                }, null, false).show();
                break;
            case R.id.rl_save:
                List<BindBean> list = mAdapter.getData();
                if (list.size() == 0)
                    return;
                mBindBean = list.get(0);
                mvpPresenter.checkIn(BaseApplication.userId, mBindBean.getQrcode());
                break;
        }
    }


    @Override
    public void onSuccess() {
        BindingHistory.insertHistory(mBindBean, true, BindBean.REQUEST_CODE_CHECK_IN, "onSuccess");
        showTips("入库成功", "");
        mAdapter.getData().clear();
    }

    @Override
    public void onFailed(String message) {
        showTips("入库失败", message);
        BindingHistory.insertHistory(mBindBean, false, BindBean.REQUEST_CODE_CHECK_IN, message);

    }


}
